
package Persistencia;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.Vector;

import Controlador.Utils;
import Negocio.Cereal;
import Negocio.CerealCosechaFina;


public class AdmPersistenciaCerealCosechaFina extends AdministradorPersistencia 
{
	private static AdmPersistenciaCerealCosechaFina instancia;
	
	private AdmPersistenciaCerealCosechaFina()
	{
		
	}
	public static AdmPersistenciaCerealCosechaFina getInstancia()
	{
		if (instancia == null)
			instancia = new AdmPersistenciaCerealCosechaFina();
		return instancia;
	}

	public int generarCodigo()
	{
		try
		{
		int cant =0, cant2 = 0;
		Connection con = PoolConnection.getPoolConnection().getConnection();
		PreparedStatement s = con.prepareStatement("Select Count(*) from cosechaFina");
		PreparedStatement r = con.prepareStatement("Select Count(*) from cosechaGruesa");
		ResultSet res=  s.executeQuery();
		ResultSet res2 = r.executeQuery();
		while (res.next())
		{
			cant = res.getInt(1);
		}
		while (res2.next())
		{
			cant2 = res2.getInt(1);
		}
		PoolConnection.getPoolConnection().realeaseConnection(con);
		return cant + cant2;
		}
		catch(Exception e)
		{
			System.out.println(e.getMessage());
			
		}
		return 0;
		
		
	}
	
	public void delete(Object d) 
	{
		try
		{
			CerealCosechaFina a = (CerealCosechaFina)d;
			Connection con = PoolConnection.getPoolConnection().getConnection();
			PreparedStatement s = con.prepareStatement("update  cosechaFina set estado = ? where codigo = ?");
			s.setString(1, Utils.CONSTANTE_INACTIVO);
			s.setLong(2, a.getCodigo());
			s.execute();
			PoolConnection.getPoolConnection().realeaseConnection(con);
		}
		catch (Exception e)
		{
			System.out.println();
		}
		

	}

	@Override
	public void insert(Object o) 
	{
		try
		{
			CerealCosechaFina a = (CerealCosechaFina)o;
			Connection con = PoolConnection.getPoolConnection().getConnection();
			PreparedStatement s = con.prepareStatement("insert into cosechaFina values (?,?,?,?,?,?)");
			//agregar campos
			s.setLong(1,a.getCodigo());
			s.setString(2, a.getNombre());
			s.setDate(3, Date.valueOf(a.getInicioEpocaSiembra()) );
			s.setDate(4, Date.valueOf(a.getFinEpocaSiembra()));
			s.setDouble(5,a.getRinde());
			s.setString(6,a.getEstado() );
			
			s.execute();
			PoolConnection.getPoolConnection().realeaseConnection(con);
		}
		catch (Exception e)
		{
			System.out.println(e.getMessage());
		}
		

	}

	@Override
	public Vector select(Object o) 
	{
		// TODO Auto-generated method stub
		return null;
	}

	
	public void update(Object o) 
	{
		try
		{
			CerealCosechaFina a = (CerealCosechaFina)o;
			Connection con = PoolConnection.getPoolConnection().getConnection();
			PreparedStatement s = con.prepareStatement("update  cosechaFina set nombre = ?, inicioEpoca = ?, finEpoca = ?, rinde = ?, estado = ? where codigo = ?" );
			
			//agregar campos
			s.setString(1, a.getNombre());
			
			s.setDate(2, Date.valueOf(a.getInicioEpocaSiembra()));

			s.setDate(3, Date.valueOf(a.getFinEpocaSiembra()));
			s.setDouble(4, a.getRinde());
			s.setString(5, a.getEstado());
			s.setLong(6, a.getCodigo());
			
			s.execute();
			PoolConnection.getPoolConnection().realeaseConnection(con);
		}
		catch (Exception e)
		{
			System.out.println();
		}
		


	}
	public CerealCosechaFina buscarCerealCosechaFina(long codigo)
	{
		try
		{
			CerealCosechaFina a = null;
			Connection con = PoolConnection.getPoolConnection().getConnection();
			PreparedStatement s = con.prepareStatement("select * from  cosechaFina where codigo = ?");			//agregar campos
			s.setLong(1,codigo);
			ResultSet result = s.executeQuery();
			while (result.next())
			{
				long cod = result.getLong(1);
				String nom = result.getString(2);
				Date ini = result.getDate(3);
				Date fin = result.getDate(4);
				double rinde = result.getDouble(5);
				String estado = result.getString(6);
				a = new CerealCosechaFina(cod,nom,ini.toString(),fin.toString(),rinde, estado);
			}
			
			PoolConnection.getPoolConnection().realeaseConnection(con);
			return a;
		}
		catch (Exception e)
		{
			System.out.println();
		}
		return null;
	}
	

}
